package com.cb.domain.service.order.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cb.common.webapp.context.RequestContext;
import com.cb.core.service.impl.GenericManagerImpl;
import com.cb.domain.model.order.OrderLog;
import com.cb.domain.model.system.SystemUser;
import com.cb.domain.model.user.UserAccount;
import com.cb.domain.dao.order.OrderLogDAO;
import com.cb.domain.service.order.OrderLogManager;
import com.cb.core.webapp.paging.PagingBean;

@Transactional
@Service
public class OrderLogManagerImpl extends GenericManagerImpl<OrderLog, Long> implements OrderLogManager{
	
	@Autowired
	private OrderLogDAO orderLogDAO;
	
	@Autowired
	public OrderLogManagerImpl(OrderLogDAO orderLogDAO) {
		super(orderLogDAO);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public List<OrderLog>getByVo(OrderLog vo,PagingBean pb){
		return ((OrderLogDAO)genericDAO).getByVo(vo, pb);
	}

	@Override
	/**
	 * 传入操作记录,操作类型,订单ID(userType:1 普通用户，userType:2 管理员用户)
	 */
	public void saveLog(String action,String userType,String orderId) {
		OrderLog log=new OrderLog();
		if(userType.equalsIgnoreCase("1")){
			UserAccount account=RequestContext.getCurrentUser();
			if(account!=null){
			log.setOperatorId(account.getUserId());
			log.setOperatorName(account.getUsername());
			}
		}else if(userType.equalsIgnoreCase("2")){
			SystemUser systemUser=RequestContext.getCurrentSystemUser();
			log.setOperatorId(systemUser.getSystemUserId());
			log.setOperatorName(systemUser.getUsername());
		}
		
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm");
		log.setOrderId(orderId);
		log.setAction(action+dateFormat.format(new Date()));
		log.setTime(new Date());
		orderLogDAO.save(log);
	}
}
